package chelydra.sharelydra.server.web.service;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import chelydra.sharelydra.server.web.SLSServlet;
import chelydra.sharelydra.server.ejb.SLSUser;
import java.util.Collection;
import java.util.StringTokenizer;
import java.util.Vector;

/**
 *
 * @author scallion
 */
public class SLSSearchServlet extends SLSServlet {

	public SLSSearchServlet() {
		this.action_name = "search";
	}

	@Override
  protected void processRequest(HttpServletRequest request, HttpServletResponse response)
  throws ServletException, IOException {
		this.initManager(request);
		SLSUser user=this.initAttributes(request,response);
		
		if (user!=null) {
			Collection<String> keywords = new Vector();

			// get the keywords for the search
			String keywordsString = request.getParameter("keywords");
			System.err.println("keywordsString::"+keywordsString);
			if (keywordsString != null) {
				StringTokenizer st =new StringTokenizer(keywordsString," ");
				while (st.hasMoreTokens()){
					keywords.add(st.nextToken());
				}
			}
			//System.err.println("keywords::"+keywords);
			Collection<SLSUser> usersFound = this.manager.searchFriendsForKeywords(keywords);
			request.setAttribute("usersFound", usersFound);

			//System.err.println(friendsFound);

		} // if user!=null

		this.rd.forward(request, response);
  }
}
